<?

if ($DATA['component']['id']) {
	
	$_record = array('id_data' => D_MB_SETTINGS, 'id_parent' => $DATA['component']['id'], 'assoc' => '');
	
	$_defaults = array('contact' => '', 'from' => 'Site Engine <sb@rulezz.com.ua>', 'check' => '351983', 'rt_name' => '', 'rt_mail' => '', 'markup' => 0);
	$_def_lang_dependent = array('caption', 'form_send', 'required', 'not_sent', 'sent');
	$_def_multilang = array(
		'en' => array(
			'caption'		=> '',
			'form_send'		=> 'Send',
			'required'		=> 'Please fill the required fields:',
			'not_sent'		=> 'Error: Mail sending failed. Please try again in a few minutes.',
			'sent'			=> 'Your message has been sent.'
		),
		'ru' => array(
			'caption'		=> '',
			'form_send'		=> 'Отправить',
			'required'		=> 'Пожалуйста заполните необходимые поля:',
			'not_sent'		=> 'Ошибка при отправке сообщения. Попробуйте еще раз через пару минут.',
			'sent'			=> 'Сообщение отправлено.'
		),
		'uk' => array(
			'caption'		=> '',
			'form_send'		=> 'Надіслати',
			'required'		=> 'Заповніть будь ласка необхідні поля:',
			'not_sent'		=> 'Помилка: Повідомлення не було відправлено. Спробуйте ще раз через декілька хвилин',
			'sent'			=> 'Ваше повідомлення було відправлено.'
		)
	);


	$_fields = array(
		'contact' => '', 
		'from' => 'Site Engine <sb@rulezz.com.ua>', 
		'check' => '351983', 
		'rt_name' => '', 
		'rt_mail' => '', 
		'markup' => 0,
		'caption' => array('large' => true, 'lang' => true),
		'form_send' => array('lang' => true),
		'required' => array('lang' => true),
		'not_sent' => array('lang' => true),
		'sent' => array('lang' => true),
	);
	


	$DATA['fields'] = array_mark_bounds( datafetch_array('db_list', array('table' => tb('data'), 'where' => "id_data=".D_MB_DATA." AND id_parent=".$DATA['component']['id'], 'order' => 'id_index', 'index' => 'id' )));

	foreach($DATA['fields'] as $k => $v) {
		$v['kind'] = $v['tag'] & 0xff;
		$v['required'] = ($v['tag'] >> 8) & 1;

		$v = array_merge($v, datafetch_array('loadmeta', D_MB_META, 
			array('parent' => $k, 'assoc' => context('lang'), 'getclob' => true)));

		$DATA['fields'][$k] = $v;
	}


	switch (action(array('editor_save'))) {
	
		case 'editor_save':

			dataset('killmeta', "id_data=".D_MB_SETTINGS." AND (id_parent={$DATA[component][id]})");
			dataset('savemeta', prepareModel($_fields, $_record));
			
			return true;	// end of component edition
	}
	

	$DATA['settings'] = array('data' => $_def_multilang);	// load defaults

	$temp = datafetch_array('db_list', array(
		'table' => tb('data'), 
		'where' => "(id_data=".D_MB_SETTINGS.") AND (id_parent={$DATA[component][id]}) AND (assoc='' OR assoc IS NULL)", 
		'index' => 'name' ));


	$DATA['settings'] = array_merge($_fields, datafetch_array('loadmeta', D_MB_SETTINGS, 
		array('parent' => $DATA['component']['id'], 'getclob' => true)));


	foreach($DATA['langs'] as $lang)
		$DATA['settings']['data'][$lang['text']] = array_merge($_def_multilang[$lang['text']], 
			datafetch_array('loadmeta', D_MB_SETTINGS, 
				array('parent' => $DATA['component']['id'], 'assoc' => $lang['text'], 'getclob' => true)));
		
		
/*

	$temp = datafetch_array('db_list', array('table' => tb('data'), 'where' => "(id_data=".D_MB_SETTINGS.") AND (id_parent={$DATA[component][id]}) AND (assoc IS NOT NULL)"));
	
	foreach($temp as $k => $v)	
		$DATA['settings']['data'][$v['assoc']][$v['name']] = $v['value'];	// load language dependent data
*/
		
	return false;

} else exit;
